package product;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GradientPaint;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JScrollPane;
import javax.swing.ImageIcon;

import java.awt.BorderLayout;

import javax.swing.JMenuBar;
import javax.swing.JLabel;

public class Stock_Chart extends JInternalFrame {
	static int count1 = 0;
	static int count2 = 6;
	static int cnt = 1;
	String dept;
	String dept1;
	String dept2,dept3;
	String s_dept,s_dept1,s_dept2,s_dept3;
	int ptot,stot;
	static Connection con;

	 ChartPanel chartpanel;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Stock_Chart frame = new Stock_Chart();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Stock_Chart() throws Exception {
		super("제품별막대그래프", true, true, true,true);
		setResizable(true);
		setTitle("제품별 막대 그래프");
		setBounds(100, 100, 582, 411);
		CategoryDataset categorydataset = createDataset();
        JFreeChart jfreechart = createChart(categorydataset);
        setJMenuBar(getMenuBar_1());
        ChartPanel chartpanel = new ChartPanel(jfreechart);
        chartpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartpanel);
        chartpanel.setLayout(new BorderLayout(0, 0));
	}   

	public static void DbCon()throws Exception{
		 Class.forName("oracle.jdbc.driver.OracleDriver");
		 System.out.println("드라이버 로딩 성공...");
	      String url="jdbc:oracle:thin:@192.168.10.4:1521:XE";
	        
	      String user="hr";
	      String pwd="hr";
	        
	     con=DriverManager.getConnection(url,user,pwd);
	      System.out.println("DB 연결 성공!");
	}
	 DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
	 private JMenuBar menuBar;
	 private JPanel panel;
	 private JButton btnNewButton;
	 private JButton btnNewButton_1;
	 
	 
	 
	 
	 
	 
	private static CategoryDataset createDataset() throws Exception{
		DbCon();
   
        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
        
        Statement st=con.createStatement();
	     
//	      String sql="select * from stock";
	      String sql="select c.pro_code,s.s_amount,c.pro_nm from stock s,code c where c.pro_code=s.s_code";
	     ResultSet rs=st.executeQuery(sql);
	      	    
	      while(rs.next()){
	    	  String dept=rs.getString(1);
	    	  int dept2=rs.getInt(2);
	    	  String dept3=rs.getString(3);
//	    	  if(count1<cnt && cnt<=count2){ 
	    	  defaultcategorydataset.addValue(dept2,"제품",dept3);//재고량,코드,
//	    	  cnt++;
//	    	  }
	      }
        return defaultcategorydataset;
    }

//public void conn()throws Exception{
//		 Class.forName("oracle.jdbc.driver.OracleDriver");
//		 System.out.println("드라이버 로딩 성공...");
//	      String url="jdbc:oracle:thin:@192.168.10.4:1521:XE";
//	        
//	      String user="hr";
//	      String pwd="hr";
//	      Connection con=DriverManager.getConnection(url,user,pwd);
//	      System.out.println("DB 연결 성공!");
//	     
//	      Statement st=con.createStatement();
//	      String sql="select * from stock";
//	      
//	      ResultSet rs=st.executeQuery(sql);
//	      while(rs.next()){
//	    	   dept=rs.getString(1);
//	    	   dept1=rs.getString(2);
//	      }
//	      rs.close();
//	      st.close();
//	      con.close();
//	}
	 private static JFreeChart createChart(CategoryDataset categorydataset){
	        JFreeChart jfreechart = ChartFactory.createBarChart("제품별 막대 그래프", "제품", "재고량",categorydataset, PlotOrientation.VERTICAL, true, true, false);
	        jfreechart.setBackgroundPaint(Color.white);
	        CategoryPlot categoryplot = jfreechart.getCategoryPlot();
	        categoryplot.setBackgroundPaint(Color.lightGray);
	        categoryplot.setDomainGridlinePaint(Color.white);
	        categoryplot.setDomainGridlinesVisible(true);
	        categoryplot.setRangeGridlinePaint(Color.white);
	        NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
	        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
	        BarRenderer barrenderer = (BarRenderer)categoryplot.getRenderer();
	        barrenderer.setDrawBarOutline(false);
	        GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F, new Color(0, 0, 64));
	        GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, new Color(0, 64, 0));
	        GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, new Color(64, 0, 0));
//	        barrenderer.setSeriesPaint(0, gradientpaint);
//	        barrenderer.setSeriesPaint(1, gradientpaint1);
//	        barrenderer.setSeriesPaint(2, gradientpaint2);
	        
	        
	        CategoryAxis categoryaxis = categoryplot.getDomainAxis();
	        
	        categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.52359877559829882D));
	        
	        Font f = new Font("굴림", Font.BOLD, 13);

			Font axisF = new Font("굴림", Font.PLAIN, 14);
			final CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator();

			final ItemLabelPosition p_center = new ItemLabelPosition(
					ItemLabelAnchor.CENTER, TextAnchor.CENTER);

			final ItemLabelPosition p_below = new ItemLabelPosition(
					ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_LEFT);

			barrenderer.setBaseItemLabelGenerator(generator);

			barrenderer.setBaseItemLabelsVisible(true);

			barrenderer.setBasePositiveItemLabelPosition(p_center);
			
	        barrenderer.setBaseItemLabelFont(f);
	        
	        categoryplot.getDomainAxis().setTickLabelFont(new Font("굴림", 0, 13));
	       //단위 넣는 코드인데 라이브러리없으므로 나타나지않습니다.
	        double domainTickUnit=1000;
	        final NumberAxis domainAxis = new NumberAxis("AAAAA");
	        domainAxis.setTickUnit( new NumberTickUnit(domainTickUnit) );
	        
	        
	        TextTitle title = jfreechart.getTitle();
	        title.setFont(new Font("굴림", 0, 20));
	        CategoryAxis titleX =categoryplot.getDomainAxis();
	        titleX.setLabelFont(new Font("돋움",0,15));
	        
	        ValueAxis titleY =categoryplot.getRangeAxis();
	        titleY.setLabelFont(new Font("돋움",0,15));
	       
	        jfreechart.getLegend().setItemFont(new Font("돋움",Font.PLAIN,15));
	        
	        return jfreechart;
	    }
	 public static JPanel createDemoPanel() throws Exception{
	        JFreeChart jfreechart = createChart(createDataset());
	        return new ChartPanel(jfreechart);
	    }
	 public static String getDemoDescription(){
	        return "A bar chart.";
	    }
	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getPanel());
		}
		return menuBar;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtnNewButton(), BorderLayout.WEST);
			panel.add(getBtnNewButton_1(), BorderLayout.EAST);
		}
		return panel;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("이전 제품들 보기");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
//					System.out.println(count1);
//					for(int i=1;i<6;i++){
//						System.out.println(i);
//						int su=count1+=i;
//						System.out.println(su);
//						}
				}
			});
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("다음 제품들보기");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					
				}
			});
		}
		return btnNewButton_1;
	}
}
